package com.paipaideli.ui.mine.safety;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.TStatusView;
import com.paipaideli.common.utils.JsutmentUtil;
import com.paipaideli.common.utils.StringUtil;
import com.paipaideli.common.utils.ToastUtil;

import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 编辑银行卡
 */
public class ModifyBankActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.et_modifybank_name)
    EditText etModifybankName;
    @BindView(R.id.et_modifybank_phone)
    EditText etModifybankPhone;
    @BindView(R.id.et_modifybank_num)
    EditText etModifybankNum;
    @BindView(R.id.et_card_type)
    EditText et_card_type;
    @BindView(R.id.et_brank_name)
    EditText et_brank_name;
    @BindView(R.id.btn_bank_add)
    Button btnBankAdd;
    @BindView(R.id.bg_statusview)
    TStatusView bg_statusview;

    private String bank;
    private String isDefault;
    private String id = "";

    @Override
    public int getContentView() {
        return R.layout.activity_modifybank;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        bank = getIntent().getStringExtra("bank");
        if (bank.equals("1")) {
            top_title.setText("添加银行卡");
        } else {
            top_title.setText("编辑银行卡");

            id = getIntent().getStringExtra("id");
            etModifybankName.setText(getIntent().getStringExtra("name"));
            etModifybankPhone.setText(getIntent().getStringExtra("phone"));
            etModifybankNum.setText(getIntent().getStringExtra("bankNumber"));
            et_card_type.setText(getIntent().getStringExtra("cardTypeName"));
            et_brank_name.setText(getIntent().getStringExtra("bankName"));
        }
        //返回
        top_back.setOnClickListener(o ->
                finish()
        );

        //添加修改银行卡
        RxView.clicks(btnBankAdd)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(o -> {
            addOrModfiyBank();
        }, throwable -> {
        });
    }

    private void addOrModfiyBank() {
        String name = etModifybankName.getText().toString().trim();
        String phone = etModifybankPhone.getText().toString().trim();
        String bank_num = etModifybankNum.getText().toString().trim();
        String cardType = et_card_type.getText().toString().trim();
        String bankName = et_brank_name.getText().toString().trim();
        String bank_lx = et_card_type.getText().toString().trim();
        String isDefault = "1";
        if (!StringUtil.isEmpty(name)) {
            if (!StringUtil.isEmpty(phone)) {
                if (!JsutmentUtil.isMobileTrue(phone)) {
                    ToastUtil.show("请输入正确的手机号！");
                    return;
                }
                if (!StringUtil.isEmpty(bank_num)) {
                    if (bank_num.length() < 10) {
                        ToastUtil.show("请输入正确的银行卡！");
                        return;
                    }
                    if (StringUtil.isEmpty(bankName)||StringUtil.isEmpty(bank_lx)){
                        ToastUtil.show("请输入银行卡的开户行和类型！");
                    }else {
                        JSONObject requestData = new JSONObject();
                        try {
                            requestData.put("name", name);
                            requestData.put("phone", phone);
                            requestData.put("bankNumber", bank_num);
                            requestData.put("cardTypeName", cardType);
                            requestData.put("bankName", bankName);
                            requestData.put("isDefault", isDefault);
                            requestData.put("id", id);
                        } catch (Exception e) {
                            e.getMessage();
                        }
                        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
                        if (bank.equals("1")) {
                            bg_statusview.showLoading();
                            Api.getInstanceGson().bankAdd(body)




                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(request -> {
                                        if (request.getCode().equals("200")) {
                                            finish();
                                        }
                                        ToastUtil.show(request.getMessage());
                                        bg_statusview.finish();
                                    }, throwable -> {
                                        bg_statusview.finish();
                                        Log.e(TAG, "error", throwable);
                                    });
                        } else {
                            bg_statusview.showLoading();
                            Api.getInstanceGson().bankModify(body)
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(request -> {
                                        if (request.getCode().equals("200")) {
                                            finish();
                                            bg_statusview.finish();
                                            ToastUtil.show(request.getMessage());
                                        } else {
                                            bg_statusview.finish();
                                            ToastUtil.show(request.getMessage());
                                        }
                                    }, throwable -> {
                                        bg_statusview.finish();
                                        Log.e(TAG, "error", throwable);
                                    });
                        }
                    }
                } else {
                    ToastUtil.show("请输入姓名！");
                }
            } else {
                ToastUtil.show("请输入手机号！");
            }
        } else {
            ToastUtil.show("请输入银行卡号！");
        }
    }
}
